var Tool = Parse.Object.extend(TABLE_USERTOOL);
var keyToShare = '';
/**********************************************
Function support edit/delete tool
***********************************************/
function setToolToShare(objKey){
    keyToShare = objKey;
}
function shareTool(objKey,userKey){
    var query = new Parse.Query(Tool); 
    //get tools
        query.get(objKey, {
          success: function(objectResult) {
            sharedTool = new Tool();
            user = new Parse.User();
            user.set({id:userKey});
            sharedTool.save({
                    name:objectResult.get('name'),
                    price:objectResult.get('price'),
                    spec:objectResult.get('price'),
                    brand:objectResult.get('brand'),
                    model:objectResult.get('model'),
                    userId:   userKey,
                    user: user
                   // ACL: new Parse.ACL()
                
            },
                {success:function (){
                    alert("Sharing success");
                    $.fancybox.close();
                    return false;
                },
                error: function(object, error) {
                alert('Error: '+error.message);            
                return false;
              }
            });
          },
          error: function(object, error) {
            alert('Error: '+error.message);            
            $.fancybox.close();
            return false;
          }
        });
        return false;
}
function showToolForm(objKey){   
    if(objKey.length<3){
         clearFormTextField('#project_tool_form',
                        Array('toolName','toolPrice','toolSpec','toolBrand','toolModel'));
                        $("#project_tool_form input[name=toolID]").val('');   
                        return;
    }
    var query = new Parse.Query(Tool); 
        query.get(objKey, {
          success: function(objectResult) {
            $("#project_tool_form input[name=toolName]").val(objectResult.get('name'));
            $("#project_tool_form input[name=toolPrice]").val(objectResult.get('price'));
            $("#project_tool_form input[name=toolSpec]").val(objectResult.get('spec'));
            $("#project_tool_form input[name=toolBrand]").val(objectResult.get('brand'));
            $("#project_tool_form input[name=toolModel]").val(objectResult.get('model'));
            $("#project_tool_form input[name=toolID]").val(objKey);         
          },
          error: function(object, error) {
            clearFormTextField('#project_tool_form',
                        Array('toolName','toolPrice','toolSpec','toolBrand','toolModel'));
                        return;
          }
        });
}

function deleteTool(objKey){
    tool = new Tool();
    tool.set({id:objKey});
    tool.destroy({
      success: function(tool) {
        // The object was deleted from the Parse Cloud.
         $("#td_tool_"+objKey).remove();
      },
      error: function(myObject, error) {
        alert('Error: '+error.message);
      }
});
}

$(document).ready(function(){
    setActive2ndMenu(2);
    var queryTool = new Parse.Query(Tool);
       queryTool.equalTo("user", _currentUser);
       queryTool.find({
          success:function(list) {
            if(list.length>0) $('#no_tool').html('');
             for(var i = 0;i<list.length;i++){
             var object = list[i];
             var html = 
            '<td>'+object.get('name')+'</td>'+
            '<td>'+object.get('spec')+'</td>'+
            '<td>'+object.get('brand')+'</td>'+
            '<td>'+object.get('price')+'</td>'+
            '<td>'+object.get('model')+'</td>'+
            '<td style="text-align:center;">'+
             '<a href ="#tool_popup_form" class="modalbox btn-item-small" onclick ="showToolForm(\''+object.id+'\');"><span class="icon-edit-small"></span></a>'+
             '<a onclick ="deleteTool(\''+object.id+'\');"  class="btn-item-small"><span class="icon-remove-small"></span></a><br/><br/>'+
              '<a href ="#tool_sharing_popup_form" class="modalbox" onclick ="setToolToShare(\''+object.id+'\');">Share</a>'+
             //tool_sharing_popup_form
             '</td>';
             /*
             <td style="text-align:center;">
                            	<a class="btn-item-small"><span class="icon-edit-small"></span></a>
                                <a class="btn-item-small"><span class="icon-remove-small"></span></a>
                            </td>
                            */
             $('#tool_list').append("'<tr id =\"td_tool_"+object.id+"\">'"+html+'</tr>');
             }
          }
          });
          
       $('#actionSaveTool').click(function(){
        tool = new Tool();;
        if($("#project_tool_form input[name=toolName]").val().length>3){
            tool.set({id:$("#project_tool_form input[name=toolID]").val()});
        }
            tool.save(
                {
                    name: $("#project_tool_form input[name=toolName]").val(),
                    price:$("#project_tool_form input[name=toolPrice]").val(),
                    spec:$("#project_tool_form input[name=toolSpec]").val(),
                    brand:$("#project_tool_form input[name=toolBrand]").val(),
                    model:$("#project_tool_form input[name=toolModel]").val(),
                    userId:   _currentUser,
                    ACL:  _aclRead   
               }, {
          success: function(object) {
            var html = 
                    '<td>'+object.get('name')+'</td>'+
                    '<td>'+object.get('spec')+'</td>'+
                    '<td>'+object.get('brand')+'</td>'+
                    '<td>'+object.get('price')+'</td>'+
                    '<td>'+object.get('model')+'</td>'+
                    '<td style="text-align:center;">'+
                     '<a href ="#tool_popup_form" class="modalbox btn-item-small" onclick ="showToolForm(\''+object.id+'\');"><span class="icon-edit-small"></span></a>'+
                     '<a onclick ="deleteTool(\''+object.id+'\');"  class="btn-item-small"><span class="icon-remove-small"></span></a><br/><br/>'+
                      '<a href ="#tool_sharing_popup_form" class="modalbox" onclick ="setToolToShare(\''+object.id+'\');">Share</a>'+
                     //tool_sharing_popup_form
                     '</td>';
           var currentID = $("#project_tool_form input[name=toolID]").val();
           if(currentID==undefined ||currentID.length<3){
                $('#tool_list').append("'<tr id =\"td_tool_"+object.id+"\">'"+html+'</tr>');
            }
            else{
                $('#td_tool_'+currentID).html(html);
            }
            $('#no_tool').html('');
             $.fancybox.close();
            return false;
            
          },
          error: function(model, error) {
            alert('Error: '+error.message);
            return false;
          }
          }); 
          return false;
    });
    $('#actionShareTool').click(function(){
        //alert('key to share '+$('#shared_user_id').val());return false;
        shareTool(keyToShare,$('#shared_user_id').val());
        return false;
    });
     displayColumnRight();
});